 /*
 * @description: 评论数据管理
 * @Author: along
 * @Date: 2019-08-24
 * @Last Modified by: along
 * @Last Modified time: 2020-08-24
 */
<template>
    <div class="container commentData">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">用户手机/ID:</p>
                    <el-input
                        v-model="formData.student_no"
                        placeholder="请输入手机号码/ID"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">评论内容:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="请输入评论内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">是否为弹幕:</p>
                    <el-select
                        v-model="formData.danmu"
                        clearable
                        filterable
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in danmuList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">审核人:</p>
                    <el-input
                        v-model="formData.verify_admin_true_name"
                        placeholder="请输入审核人"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">大学名称:</p>
                    <el-cascader
                        placeholder="请选择"
                        :options="school_tree"
                        filterable
                        clearable
                        change-on-select
                        expand-trigger="click"
                        v-model="formData.school_name"
                        @change="getTableData(1)"
                        :props="{
                            label: 'name',
                            value: 'id',
                            children: 'children'
                        }"
                        size="small"
                        style="height:32px;padding-left:8px"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">发布时间:</p>
                    <el-date-picker
                        v-model="date"
                        type="datetimerange"
                        :picker-options="pickerOptions()"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        align="right"
                        size="small"
                        @change="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                        :style="{padding: '9px 23px'}"
                    >
                        搜索
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="commentData"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="praise_no"
                label="夸奖编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            />
            <el-table-column
                prop="student.student_no"
                label="用户编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            />
            <el-table-column
                prop="student.true_name"
                label="用户昵称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="student.phone"
                label="用户电话"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            />
            <el-table-column
                prop="student.school"
                label="学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="tags"
                label="标签"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.tags.length > 0">
                        {{ scope.row.tags.join(',') }}
                    </p>
                    <p v-else>
                        --
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="content"
                label="评论详情"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            />
            <el-table-column
                prop="images"
                label="图片"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p
                        v-if="scope.row.images.length > 0"
                        class="cursor-font"
                        @click="fnShowPreview(scope.row.images, 0)"
                    >
                        查看({{ scope.row.images.length }})
                    </p>
                    <p v-else>
                        --
                    </p>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                label="发布时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="is_anonymous"
                label="是否匿名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.is_anonymous == 1">是</p>
                    <p v-if="scope.row.is_anonymous == 0">否</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="good_count"
                label="点赞量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
                :width="120"
            >
                <template slot-scope="scope">
                    {{ scope.row.good_count | filterCount }}
                </template>
            </el-table-column>
            <el-table-column
                prop="is_danmaku"
                label="是否为弹幕"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.is_danmaku"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnSetCommend(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="is_delete"
                label="是否删除"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.is_delete == 0">未删除</p>
                    <p v-if="scope.row.is_delete == 1">已删除</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.status == 0">下架</p>
                    <p v-if="scope.row.status == 1">正常</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="verify_admin_true_name"
                label="审核人"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                label="操作"
                align="center"
                fixed="right"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        :style="{marginLeft: 0}"
                        @click="fnShelves(scope.row)"
                    >
                        {{ scope.row.status == 1 ? '下架' : '上架' }}
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnToping(scope.row)"
                        :style="{marginLeft: 0}"
                    >
                        {{ !scope.row.is_top ? '置顶' : '取消置顶' }}
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnCheckVerfy(scope.row)"
                        :style="{marginLeft: 0}"
                    >
                        查看
                    </el-button>
                    <el-button
                        type="text"
                        :disabled="scope.row.set_free_coupon === 1 || scope.row.set_free_coupon === -1"
                        :style="{marginLeft: 0}"
                        @click="handleGrantCoupon(scope.row)"
                    >
                        {{ scope.row.set_free_coupon === 0 ? '发券' : scope.row.set_free_coupon === 1 ? '已发券' : '不可发券' }}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 查看置顶详情 -->
        <el-dialog
            title="置顶详情"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="dialogShelvesDetail"
        >
            <div class="detail">
                <div class="detail-item">
                    操作人:  {{ verify_admin_true_name ? verify_admin_true_name : '--' }}
                </div>
                <div class="detail-item">
                    操作时间:  {{ verify_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </div>
            </div>
        </el-dialog>

        <!-- 下架弹窗 -->
        <!-- <el-dialog
            :title="shlver_status ? '下架评论' : '查看评论'"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="dialogShelves"
        >
            <div class="shelves" >
                <div class="shelves-wrap">
                    下架理由:
                    <el-input
                        v-model="shlver_reson"
                        size="small"
                        placeholder="请输入下架理由"
                        :style="{width: '400px',marginLeft: '15px'}"
                        :disabled="!shlver_status"
                    />
                </div>
                <div class="shelves-wrap">
                    操作者:
                    <p :style="{paddingLeft: '30px'}">{{ userInfo.true_name }}</p>
                </div>
            </div>
            <div
                class="zd-foot"
                v-if="shlver_status"
            >
                <el-button
                    size="small"
                    :style="{width: '120px', height: '32px'}"
                    type="default"
                    @click="dialogShelves = false"
                >
                    取 消
                </el-button>
                <el-button
                    size="small"
                    :style="{width: '120px', height: '32px', marginLeft: '60px'}"
                    type="primary"
                    :disabled="!shlver_reson"
                    @click="fnConfirmShelves()"
                >
                    确 定
                </el-button>
            </div>
        </el-dialog> -->

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
import { getDate } from '@/assets/js/utils';
import { mapState } from 'vuex';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            verify_admin_true_name: '',//置顶人
            verify_time: '',//置顶操作时间
            dialogShelvesDetail: false,//置顶详情弹窗
            student_no: '',
            shlver_status: null,//下架弹窗状态
            shlver_reson: '',
            dialogShelves: false,
            //筛选字段
            formData: {
                student_no: '',
                keyword: '',
                danmu: 99,
                verify_admin_true_name: '',
                school_name: [],
                date: []
            },
            date: [],
            danmuList: [{value: 99,label: '全部'},{value: 1,label: '是'},{value: 0,label: '否'}],
            filterData: {
                sort_field: 0,
                sort_type: 1
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            school_tree: [],
            tableData: [],
            authResult: {}
        };
    },
    filters: {
        filterCount (val) {
            if(val <= 9999) return val;
            if(val >= 10000) return (val/1000).toFixed(2) + 'w';
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userinfo.userinfo,
        })
    },
    watch: {
        dialogShelves (val) {
            if(!val) {
                this.shlver_reson = '';
            }
        }
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '社交'},{title: '夸夸我的大学'},{title: '评论数据管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getSchoolTree();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/social_schoolpraise%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword_student_id: this.formData.student_no,
                content: this.formData.keyword,
                is_danmaku: this.formData.danmu,
                verify_admin_true_name: this.formData.verify_admin_true_name,
                school_id: this.formData.school_name.length >= 3 ? this.formData.school_name[2] : '',
                create_start_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[0]))),
                create_end_time: this.date === null ? '' : (this.date.length == 0 ? '' : getDate(new Date(this.date[1]))),
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 置顶/查看
         */
        fnToping (row) {
            let value = row.is_top ? '是否要取消置顶该评论?' : '是否要置顶该评论';

            this.$confirm(`${value}`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/social_schoolpraise%5Cset_top',{
                    praise_no: row.praise_no,
                    type: row.is_top ? 0 : 1
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '置顶成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 查看审核信息
         */
        fnCheckVerfy (row) {
            this.verify_admin_true_name = row.top_admin_true_name;
            this.verify_time = row.top_set_time;
            this.dialogShelvesDetail = !this.dialogShelvesDetail;
        },

        /**
         * @description 发券
         */
        handleGrantCoupon(row) {
            if(!this.authResult.auth_grant) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限管理员',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }
            this.$confirm('是否确认发券?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/social_schoolpraise%5Cset_free_coupon',{
                    praise_no: row.praise_no
                }, resp => {
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            });
        },

        /**
         * @description 打开下架弹窗
         */
        fnShelves (row) {
            // if(!row.status) {
            //     this.shlver_reson = row.shlver_reson;
            // }
            // this.shlver_status = row.shelves ? true : false;
            // this.student_no = row.student_no;
            // this.dialogShelves = !this.dialogShelves;

            if(!this.authResult.auth_status) {
                this.$notify({
                    title: '提示',
                    message: '此权限仅限管理员',
                    type: 'warning',
                    duration: 2000
                });
                return;
            }

            if(row.status == 1) {
                this.$prompt('请输入下架原因', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\S/,
                    inputErrorMessage: '内容不可以为空'
                }).then(({ value }) => {
                    this.$post('/student/social_schoolpraise%5Cset_status',{
                        praise_no: row.praise_no,
                        status: 0,
                        disable_reason: value
                    }, resp => {
                        if (resp.code == 1) {
                            this.getTableData();
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                type: 'success',
                                duration: 2000
                            });
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                type: 'warning',
                                duration: 2000
                            });
                        }
                    });
                });
            }

            if(row.status == 0) {
                this.$post('/student/social_schoolpraise%5Cset_status',{
                    praise_no: row.praise_no,
                    status: 1,
                    disable_reason: ''
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            message: '操作成功',
                            type: 'success',
                            duration: 2000
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            type: 'warning',
                            duration: 2000
                        });
                    }
                });
            }
        },

        /**
         * @description 确定下架
         */
        // fnConfirmShelves () {
        //     this.$post('',{
        //         //
        //     },resp=>{
        //         if (resp.code == 1) {
        //             this.getTableData();
        //             this.$notify({
        //                 title: '提示',
        //                 message: '操作成功',
        //                 type: 'success',
        //                 duration: 2000
        //             });
        //         } else if (resp.code == 0) {
        //             this.$notify({
        //                 title: '提示',
        //                 message: resp.msg,
        //                 type: 'warning',
        //                 duration: 2000
        //             });
        //         }
        //     });
        // },

        /**
         * 设置弹幕
         */
        fnSetCommend (row) {
            this.$post('/student/social_schoolpraise%5Cset_danmaku',{
                praise_no: row.praise_no,
                is_danmaku: row.is_danmaku
            }, resp=> {
                if (resp.code == 1) {
                    this.$set(row, 'is_danmaku', row.is_danmaku);
                    this.$notify({
                        title: '提示',
                        message: '设置成功',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            let sortFieldMap = {
                good_count: 1
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code == 1) {
                    this.school_tree = resp.data.tree;
                } else {
                    this.$message({
                        type: 'warning',
                        message: '此权限,仅限超级管理员'
                    });
                }
            });
        },

        /**
         * @description 时间处理
         */
        pickerOptions () {
            return {
                shortcuts : [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();

                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            };
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .shelves {
        display: flex;
        flex-direction: column;
        .shelves-wrap {
            display: flex;
            align-items: center;
            height: 32px;
            margin-bottom: 20px;
        }
    }
    .zd-foot {
        display: flex;
        justify-content: center;
    }
    .detail {
        display: flex;
        flex-direction: column;
        &-item {
            height: 32px;
            width: 100%;
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            color: #333;
            font-size: 15px;
        }
    }
</style>
<style lang="less">
    .commentData{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-button {
            padding: 3px;
        }
    }
</style>
